package org.inote.service.impl;

import org.inote.dao.UserMapper;
import org.inote.pojo.tablePojo.Attention;
import org.inote.pojo.tablePojo.User;
import org.inote.service.UserService;
import org.inote.utils.StringFormUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public boolean register(String phone, String password) {
        if (StringFormUtil.isEmpty(phone, password)){
            return false;
        }
        User user = new User();
        user.setUserPhone(phone);
        user.setUserPassword(password);
        boolean success = true;
        try {
            userMapper.addUser(user);
        } catch(Exception e) {
            System.out.println("手机号已被注册");
            success =  false;
        }
        return success;
    }

    @Override
    public boolean phoneExist(String phone) {
        User user = userMapper.getUserByPhone(phone);
        if (user == null){
            return false;
        }
        return true;
    }

    @Override
    public User getUser(String phone) {
//        User user = userMapper.getUserByPhone(phone);
//        String realImg = "/upload/";
//        realImg += user.getUserImg();
//        user.setUserImg(realImg);
        return userMapper.getUserByPhone(phone);
    }

    @Override
    public boolean updatePasswordById(Integer id, String newPassword) {
        System.out.println(id);
        System.out.println(newPassword);
        int i = userMapper.updatePasswordById(id, newPassword);
        if (i<=0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public List<User> queryMyAttentionUsers(Integer userId) {
        return userMapper.queryMyAttentionUsers(userId);
    }

    @Override
    public Integer queryMyAttentionCount(Integer userId) {
        return userMapper.queryMyAttentionCount(userId);
    }

    @Override
    public User queryUserInfoByUserId(Integer userId) {
       // User user = userMapper.queryUserById(userId);
       // String realImg = "/upload/";
       // realImg += user.getUserImg();
       // user.setUserImg(realImg);
        return userMapper.queryUserById(userId);
    }

    /**
     * 关注用户
     * @param attention
     * @return
     */
    @Override
    public boolean addMyAttentionUser(Attention attention) {
        return userMapper.addMyAttentionUser(attention);
    }
}
